package dao.Impl;

import context.BaseContext;
import dao.BaseDao;
import dao.EmployeeDao;
import model.entity.Employee;
import util.Encrypt;
import util.NowTime;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDaoImpl extends BaseDao implements EmployeeDao {


    @Override
    public Employee Find(int id) {
        Employee employee=new Employee();
        String sql="select * from employee where id=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                employee.setCreateTime(String.valueOf(rs.getTimestamp("create_time")));
                employee.setId(rs.getInt("id"));
                employee.setName(rs.getString("name"));
                employee.setUsername(rs.getString("username"));
                employee.setPassword(rs.getString("password"));
                employee.setPhone(rs.getString("phone"));
                employee.setGender(rs.getString("gender"));
                employee.setIdNumber(rs.getString("id_number"));
                employee.setStatus(rs.getInt("status"));
                employee.setUpdateTime(String.valueOf(rs.getTimestamp("update_time")));
                employee.setCreatUser(rs.getLong("create_user"));
                employee.setUpdateUser(rs.getLong("update_user"));
            }
        }
        catch (SQLException e){
            System.out.println("按id查询错误！"+e);
        }
        return employee;
    }

    @Override
    public Employee Find(String username) {
        Employee employee=new Employee();
        String sql="select * from employee where username=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()){
                employee.setCreateTime(String.valueOf(rs.getTimestamp("create_time")));
                employee.setId(rs.getInt("id"));
                employee.setName(rs.getString("name"));
                employee.setUsername(rs.getString("username"));
                employee.setPassword(rs.getString("password"));
                employee.setPhone(rs.getString("phone"));
                employee.setGender(rs.getString("gender"));
                employee.setIdNumber(rs.getString("id_number"));
                employee.setStatus(rs.getInt("status"));
                employee.setUpdateTime(String.valueOf(rs.getTimestamp("update_time")));
                employee.setCreatUser(rs.getLong("create_user"));
                employee.setUpdateUser(rs.getLong("update_user"));
            }
        }
        catch (SQLException e){
            System.out.println("根据username查询员工失败！"+e);
        }
        return employee;
    }

    @Override
    public void creatEmployee(String idNumber, String name, String phone, String gender, String username,int user) {
        String sql="insert into employee (id_number,name,phone,gender,username,password,status,create_time,create_user,update_time,update_user) value (?,?,?,?,?,?,?,?,?,?,?);";
        //获取当前时间
        NowTime time=new NowTime();
        String t=time.getTime();
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,idNumber);
            pstmt.setString(2,name);
            pstmt.setString(3,phone);
            pstmt.setString(4,gender);
            pstmt.setString(5,username);
            //默认密码为123
            pstmt.setString(6,Encrypt.toMd5("123"));
            pstmt.setInt(7,1);
            pstmt.setTimestamp(8, Timestamp.valueOf(t));
            pstmt.setInt(9,user);
            pstmt.setTimestamp(10, Timestamp.valueOf(t));
            pstmt.setInt(11,user);
            pstmt.executeUpdate();
            System.out.println("新增员工成功!");
        }
        catch (SQLException E){
            System.out.println("新增员工失败!"+E);
        }
    }

    @Override
    public void UpdateStatus(int id, int status) {
        String sql="update employee set status=? where id=?;";
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            pstmt.setInt(2,id);
            pstmt.executeUpdate();
        }
        catch (SQLException e){
            System.out.println("修改员工状态失败!"+e);
        }
    }

    @Override
    public int FindStatus(int id) {
        String sql="select status from employee where id=?;";
        int status = 1;
        try {
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()) {
                status = rs.getInt(1);
            }
        }
        catch (SQLException e){
            System.out.println("查询员工状态失败!");
        }
        return status;
    }

    @Override
    public void Update(int id, String idNumber, String name, String phone, String gender, String username,int user) {
        NowTime time=new NowTime();
        String t=time.getTime();
        String sql=" update employee set id_number=?,name=?,phone=?,gender=?,username=?,update_user=?,update_time=? where id=?;";
        try{
            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,idNumber);
            pstmt.setString(2,name);
            pstmt.setString(3,phone);
            pstmt.setString(4,gender);
            pstmt.setString(5,username);
            pstmt.setInt(6, user);
            pstmt.setTimestamp(7, Timestamp.valueOf(t));
            pstmt.setInt(8,id);
            pstmt.executeUpdate();

        }
        catch (SQLException e){
            System.out.println("修改员工信息失败!"+e);
        }
    }

    @Override
    public void EditPassword(int empId, String newPassword) {
        String sql="update employee set password=? where id=?";
        try{
            pstmt= conn.prepareStatement(sql);
            pstmt.setInt(2,empId);
            pstmt.setString(1,Encrypt.toMd5(newPassword));
            pstmt.executeUpdate();
        }
        catch (SQLException e){
            System.out.println("修改员工密码失败!"+e);
        }
    }
//模糊查询
    @Override
    public List<Employee> All() {
        List<Employee> employeeList = new ArrayList<>();
        String sql = "SELECT * FROM employee";
//        if (employee != null) {
//            sql += " WHERE 1=1";
//            if (employee.getId() != 0) {
//                sql += " AND id='" + employee.getId() + "'";
//            }
//            if (employee.getName() != null && !employee.getName().isEmpty()) {
//                sql += " AND name LIKE '%" + employee.getName() + "%'";
//            }
//        }
        sql += " ORDER BY id DESC";
        System.out.println("查询所有: " + sql);
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.executeQuery();
            rs=pstmt.getResultSet();
            while (rs.next()) {
                Employee e = new Employee();
                e.setCreateTime(String.valueOf(rs.getTimestamp("create_time")));
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setUsername(rs.getString("username"));
                e.setPassword(rs.getString("password"));
                e.setPhone(rs.getString("phone"));
                e.setGender(rs.getString("gender"));
                e.setIdNumber(rs.getString("id_number"));
                e.setStatus(rs.getInt("status"));
                e.setUpdateTime(String.valueOf(rs.getTimestamp("update_time")));
                e.setCreatUser(rs.getLong("create_user"));
                e.setUpdateUser(rs.getLong("update_user"));
                employeeList.add(e);
            }
        } catch (SQLException e) {
            System.out.println("查询员工出错：" + sql + "," + e.getMessage());
        }
        return employeeList;
    }

    @Override
    public List<Employee> Page(int page, int pageSize) {
        page=page-1;
        page=page*pageSize;
        System.out.println("page起始"+page);
        List<Employee> employeeList = new ArrayList<>();
        String sql = "SELECT * FROM employee ORDER BY id DESC LIMIT ?,?";
        System.out.println("分页查询find(employee) : " + sql);
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,page);
            pstmt.setInt(2,pageSize);
            pstmt.executeQuery();
            rs= pstmt.getResultSet();
            while (rs.next()) {
                Employee e = new Employee();
                e.setCreateTime(String.valueOf(rs.getTimestamp("create_time")));
                e.setId(rs.getInt("id"));
                e.setName(rs.getString("name"));
                e.setUsername(rs.getString("username"));
                e.setPassword(rs.getString("password"));
                e.setPhone(rs.getString("phone"));
                e.setGender(rs.getString("gender"));
                e.setIdNumber(rs.getString("id_number"));
                e.setStatus(rs.getInt("status"));
                e.setUpdateTime(String.valueOf(rs.getTimestamp("update_time")));
                e.setCreatUser(rs.getLong("create_user"));
                e.setUpdateUser(rs.getLong("update_user"));
                employeeList.add(e);
            }
        } catch (SQLException e) {
            System.out.println("分页查询员工出错：" + sql + "," + e.getMessage());
        }
        return employeeList;
    }


}

